package com.inspectortime.webapp.inspector;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.inspectortime.domain.Employee;
import com.inspectortime.domain.Inspection;
import com.inspectortime.domain.Inspector;
import com.inspectortime.domain.User;
import com.inspectortime.repository.Repository;

public class DeleteEmployeeController extends AbstractController {
	
	private final static String ERROR_KEY = "deletionError";
	private final static String EMPLOYEE_KEY = "employee";
	
	private Repository repository;
	private String successView;
	private String confirmView;


	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String userId = request.getParameter("id");
		User user = repository.loadUser(userId);
		
		//This shouldn't ever happen
		if (user == null) {
			throw new Exception("Tried to delete a non-existent User.\nId: " + userId);
		}
		
		if ("POST".equalsIgnoreCase(request.getMethod())) {
			return processPost(request, response, user);
		} else {
			return processGet(request, response, user);
		}
		
	}
	
		
	private ModelAndView processGet(HttpServletRequest request, HttpServletResponse response, User user) {

		ModelAndView mv = new ModelAndView(this.confirmView);
		mv.addObject(EMPLOYEE_KEY, user);

		return mv;
	}


	private ModelAndView processPost(HttpServletRequest request, HttpServletResponse response, User user) {

		ModelAndView mv = null;
		
		if (user instanceof Inspector) {

			Inspector inspector = (Inspector) user;
			if (!isInspectorDeletionAllowed(inspector)) {

				mv = new ModelAndView(this.confirmView);
				mv.addObject(EMPLOYEE_KEY, user);
				mv.addObject(ERROR_KEY, "cannot.delete.inspector");
				return mv;
			}
		}
		
		Employee employee = (Employee) user;
		
		mv = new ModelAndView(this.successView);
		employee.setStatus( Employee.Status.DELETED);
		
		// To avoid future registrations and possible collisions let's change email address and username
		String appendString = "-" + RandomStringUtils.randomAlphanumeric(3) + "-DELETED";
		employee.setEmail(employee.getEmail() + appendString);
		employee.setEmailConfirm(employee.getEmailConfirm() + appendString);
		employee.setUserName(employee.getUserName() + appendString);
		
		employee.save();
	
		return mv;
		
	}


	private boolean isInspectorDeletionAllowed(Inspector inspector) {
		List<Inspection> futureInspections = inspector.findInspectionsBetween(Calendar.getInstance().getTime(), null);
		return futureInspections.isEmpty();
	}

	public void setRepository(Repository repository) {
		this.repository = repository;
	}

	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	public void setConfirmView(String confirmView) {
		this.confirmView = confirmView;
	}

}
